package com.project.service;

import com.project.dao.TeacherInfoMapper;
import com.project.entity.Department;
import com.project.entity.TeacherInfo;
import com.project.entity.User;
import com.project.myBeans.TeacherInfoDetail;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CrypDataHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 15:09
 * @param:
 * @return:
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class TeacherInfoServiceImpl extends BaseService implements TeacherInfoService {
    @Autowired
    private CrypDataHelper crypDataHelper;

    @Autowired
    private UserService userService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private UniversityService universityService;

    @Autowired
    private  TeacherInfoMapper teacherInofMapper;

    @Override
    public int add(TeacherInfo TeacherInfo) {
        return teacherInofMapper.insertSelective(TeacherInfo);
    }

    @Override
    public int removeByUserId(Integer userId) {
        return teacherInofMapper.deleteByUserId(userId);
    }

    /**
     * author: WheelChen
     * date: 2017/6/24
     * function: 根据id修改教师信息
     *
     * @param TeacherInfo
     * @return
     */
    @Override
    public int modifyById(TeacherInfo TeacherInfo) {
        return teacherInofMapper.updateByPrimaryKeySelective(TeacherInfo);
    }

    @Override
    public int modifyByUserId(TeacherInfo TeacherInfo) {
        return teacherInofMapper.updateByPrimaryKeySelective(TeacherInfo);
    }

    @Override
    public TeacherInfo findOne(Integer id) {
        return teacherInofMapper.selectByPrimaryKey(id);
    }

    @Override
    public TeacherInfo findOneByUserId(Integer userid) {
        return teacherInofMapper.selectByUserId(userid);
    }

    /**
     * author: WheelChen
     * date: 2017/7/3
     * function: 根据userId查找教师详情
     *
     * @param userId
     * @return
     */
    @Override
    public TeacherInfoDetail findOneDetailByUserId(Integer userId) throws Exception {
        TeacherInfoDetail teacherInfoDetail = new TeacherInfoDetail();
        User user ;
        //查找用户表信息
        user = userService.findOneByUserID(userId);
        teacherInfoDetail.setUser(user);
        //查找教师表信息
        TeacherInfo teacherInfo = findOneByUserId(userId);
        teacherInfoDetail.setTeacherInfo(teacherInfo);
        Department department = null;
        if (teacherInfo != null) {
            //系表
            if (teacherInfo.getDepartmentid() != null)
                department = departmentService.findOne(teacherInfo.getDepartmentid());
            //学院名称
            if (teacherInfo.getSchoolid() != null)
                teacherInfoDetail.setSchoolName(schoolService.findOne(teacherInfo.getSchoolid()).getName());
            if (department != null) {
                teacherInfoDetail.setDepartment(department);
                //获取大学名称
                if (department.getUniversityid() != null)
                    teacherInfoDetail.setUniversityName(universityService.findOne(department.getUniversityid()).getName());
            }
        }
        return teacherInfoDetail;
    }

    /**
     * author: WheelChen
     * date: 2017/7/15
     * function: 根据
     *
     * @param universityid 大学id
     * @return
     */
    @Override
    public List<TeacherInfoDetail> findBatchByUniversityid(Integer universityid) throws Exception {
        List<TeacherInfoDetail> teacherInfoDetails = teacherInofMapper.selectBatchByUniversityid(universityid);
        for (TeacherInfoDetail teacherInfoDetail: teacherInfoDetails){
            User user = teacherInfoDetail.getUser();
            crypDataHelper.decUser(user);
            teacherInfoDetail.setUser(user);
        }
        return teacherInfoDetails;
    }

    /**
     * author: WheelChen
     * date: 2017/7/25
     * function: 根据学院id查找老师详细信息
     *
     * @param teacherInfo
     * @return
     */
    @Override
    public List<TeacherInfoDetail> findBatchBySchoolIdCondition(TeacherInfo teacherInfo) throws Exception {
        List<TeacherInfoDetail> teacherInfoDetailList = teacherInofMapper.selectDetailBatchBySchoolIdCondition(teacherInfo);
        for (TeacherInfoDetail teacherInfoDetail :teacherInfoDetailList){
            User user = teacherInfoDetail.getUser();
            //解密敏感信息
            User user1 = crypDataHelper.decUser(user);
            filterSensitiveInfo(user1);
            teacherInfoDetail.setUser(user1);
        }
        return teacherInfoDetailList;
    }

    /** Todo 如何判断更新是否成功
     * author: WheelChen
     * date: 2017/7/28
     * function: 根据userId更新用户表信息 创建教师信息表信息
     *
     * @param teacherInfoDetail 教师详细信息（用户表 + 教师信息表）
     * @return
     */
    @Override
    public int addDetailOne(TeacherInfoDetail teacherInfoDetail) throws Exception {
        //update用户表 根据id
        if (teacherInfoDetail.getUser() != null)
            userService.updateUser(teacherInfoDetail.getUser());
        //insert教师信息表
        if (teacherInfoDetail.getTeacherInfo() != null)
            add(teacherInfoDetail.getTeacherInfo());
        return 0;
    }

    @Override
    public int modifyDetailOne(TeacherInfoDetail teacherInfoDetail) throws Exception {
        //update用户表 根据id
        if (teacherInfoDetail.getUser() != null)
            userService.updateUser(teacherInfoDetail.getUser());
        //update教师信息表 根据id
        if (teacherInfoDetail.getTeacherInfo() != null)
            modifyById(teacherInfoDetail.getTeacherInfo());
        return 0;
    }


}
